package com.tzxs.pay.main.gather;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.CalendarView;
import android.widget.Toast;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivitySearchGaBinding;
import com.tzxs.pay.main.base.BaseActivity;
import com.tzxs.pay.util.StatusBarColorSetter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 收款查询
 */
public class SearchGaActivity extends BaseActivity<ActivitySearchGaBinding> implements View.OnClickListener {
    private String startTimeStr;//开始时间
    private String endTimeStr;//结束时间

    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_ga;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarColorSetter.setStatusBarColor(this, Color.parseColor("#FAFAFA"));
        dataBinding.searchGaToolbar.setTitle("");
        dataBinding.searchGaBtn.setOnClickListener(this);
        setSupportActionBar(dataBinding.searchGaToolbar);
        dataBinding.searchGaToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.searchGaStartTime.setOnClickListener(this);
        dataBinding.searchGaEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_ga_btn://搜索
                String orderId = dataBinding.searchGaOrderId.getText().toString().trim();
                String note = dataBinding.searchGaNoteInfo.getText().toString().trim();
                String startTime = dataBinding.searchGaStartTime.getText().toString().trim();
                String endTime = dataBinding.searchGaEndTime.getText().toString().trim();
                if (TextUtils.isEmpty(orderId) &&
                        TextUtils.isEmpty(note) &&
                        TextUtils.isEmpty(startTime) &&
                        TextUtils.isEmpty(endTime)) {

                    Toast.makeText(this, "请输入至少一个搜索条件", Toast.LENGTH_SHORT).show();
                    return;
                }

                //判断结束时间和开始时间段的大小
                if (timeJudge(endTime) < timeJudge(startTime)) {
                    Toast.makeText(this, "结束时间不能小于开始时间", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(this, SearchResultActivity.class);
                intent.putExtra("name", "coll");//收款和分期查询
                intent.putExtra("orderId", orderId);
                intent.putExtra("note", note);
                intent.putExtra("startTime", startTime);
                intent.putExtra("endTime", endTime);

                startActivity(intent);
                break;
            case R.id.search_ga_start_time:
                chooseTime(R.id.search_ga_start_time);
                break;
            case R.id.search_ga_end_time:
                chooseTime(R.id.search_ga_end_time);

                break;
        }


    }

    //判断时间段选择
    private long timeJudge(String time) {
        SimpleDateFormat simpleDateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
        simpleDateFormat.applyPattern("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(time);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0;
    }


    /**
     * 选择时间
     *
     * @param id 控件id
     */
    private void chooseTime(final int id) {
        final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
        View content = getLayoutInflater().inflate(R.layout.time_choose, null);
        CalendarView calendarView = content.findViewById(R.id.order_time_calendarView);
        bottomSheetDialog.setContentView(content);
        bottomSheetDialog.show();
        calendarView.setOnDateChangeListener((view, year, month, dayOfMonth) -> {
            bottomSheetDialog.dismiss();

            String mM;
            String day;
            if (dayOfMonth < 10) {//日
                day = "0" + String.valueOf(dayOfMonth);
            } else {
                day = String.valueOf(dayOfMonth);
            }

            if (month < 10) {
                mM = "0" + String.valueOf(month + 1);
            } else {
                mM = String.valueOf(month + 1);
            }
            switch (id) {
                case R.id.search_ga_start_time:
                    startTimeStr = year + "-" + mM + "-" + day;
                    dataBinding.searchGaStartTime.setText(startTimeStr);
                    break;
                case R.id.search_ga_end_time:
                    endTimeStr = year + "-" + mM + "-" + day;
                    dataBinding.searchGaEndTime.setText(endTimeStr);
                    break;
            }
        });
    }

}
